package com.source.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * @author fengq
 * 自动填充配置
 */
@Slf4j
@Component
public class MybatisPlusHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("创建记录，开启自动填充...");
        //如果存在在当前值就不覆盖,而提供的是为Null也不填充
        this.fillStrategy(metaObject,"isDelete",false);
        this.fillStrategy(metaObject,"createDate",new Date());
        log.info("创建记录，自动填充完成...");
    }


    @Override
    public void updateFill(MetaObject metaObject) {
        //无论是否有值，都会自动填充数据
        log.info("更新记录，开启自动填充...");
        //this.setFieldValByName("updateDate",LocalDateTime.now(ZoneId.of("CTT")),metaObject);
        //this.strictUpdateFill(metaObject, "updateDate", LocalDateTime::now, LocalDateTime.class);
        this.strictUpdateFill(metaObject, "updateDate", Date.class, new Date());
        this.setFieldValByName("isRead",1,metaObject);
        log.info("更新记录，自动填充完成...");
    }
}
